package com.mdtx.business.controller;

import com.github.pagehelper.PageInfo;
import com.mdtx.base.ResultData;
import com.mdtx.base.SystemErrorCode;
import com.mdtx.base.exception.ControllerException;
import com.mdtx.business.entity.po.Trip;
import com.mdtx.business.service.TestService;
import com.mdtx.utils.LogHelper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 测试文件所在的位置，哈哈哈哈哈，你在哪啊，我在这啊
 *
 * @author 猫啊
 * @create 2017-05-18 23:24
 **/
@RestController
@RequestMapping("/test")
public class TestController {


    @Autowired
    private TestService service;

    @ApiOperation(value = "查询出行信息",notes = "查询出行信息")
    @RequestMapping(value = "/findAll",method = RequestMethod.POST)
    public ResultData<PageInfo<Trip>> findAll(@ApiParam(name = "trip",value = "查询条件") @RequestBody Trip trip){
        ResultData<PageInfo<Trip>> rt = new ResultData<>();
        try {
            List<Trip> list = service.findAll(trip);

            rt.setData(new PageInfo<Trip>(list));
        } catch (Exception e) {
          //  LogHelper.error("",e,TestController.class);
           throw new ControllerException(SystemErrorCode.SYS_ERR,e);
        }

        return rt;

    }

    @ApiOperation(value = "出行信息",notes = "保存出行信息")
    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    public ResultData<Trip> get(@ApiParam(name = "id",value = "出行id") @PathVariable Long id){

        ResultData<Trip> rt = new ResultData<>();
        Map m= null;

        try {
            Trip t=service.get(id);
            rt.setData(t);
        } catch (Exception e) {
            throw new ControllerException(SystemErrorCode.SYS_ERR,e);
        }


        return rt;

    }

    @ApiOperation(value = "保存出行信息",notes = "保存出行信息")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ResultData save(@ApiParam(name = "trip",value = "保存参数") @RequestBody Trip trip){

        ResultData<PageInfo<Trip>> rt = new ResultData<>();

        try {
            service.save(trip);
        } catch (Exception e) {
            throw new ControllerException(SystemErrorCode.SYS_ERR,e);
        }


        return rt;

    }

    @ApiOperation(value = "批量保存出行信息",notes = "保存出行信息")
    @RequestMapping(value = "/saveBac",method = RequestMethod.POST)
    public ResultData saveBac(@ApiParam(name = "trip",value = "保存参数") @RequestBody List<Trip> trips){

        ResultData<PageInfo<Trip>> rt = new ResultData<>();

        try {
            service.saveAll(trips);
        } catch (Exception e) {
            throw new ControllerException(SystemErrorCode.SYS_ERR,e);
        }


        return rt;

    }


    @ApiOperation(value = "修改出行信息",notes = "保存出行信息")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ResultData update(@ApiParam(name = "trip",value = "保存参数") @RequestBody Trip trip){

        ResultData<PageInfo<Trip>> rt = new ResultData<>();

        try {
            service.update(trip);
        } catch (Exception e) {
            throw new ControllerException(SystemErrorCode.SYS_ERR,e);
        }


        return rt;

    }


    @ApiOperation(value = "删除出行信息",notes = "保存出行信息")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public ResultData delete(@ApiParam(name = "id",value = "出行id") @PathVariable Long id){

        ResultData<PageInfo<Trip>> rt = new ResultData<>();

        try {
            service.delete(id);
        } catch (Exception e) {
            throw new ControllerException(SystemErrorCode.SYS_ERR,e);
        }


        return rt;

    }

    @ApiOperation(value = "批量删除出行信息",notes = "保存出行信息")
    @RequestMapping(value = "/deleteBac",method = RequestMethod.POST)
    public ResultData deleteBac(@ApiParam(name = "ids",value = "保存参数") @RequestBody List<Long> ids){

        ResultData<PageInfo<Trip>> rt = new ResultData<>();

        try {
            service.deleteAll(ids);
        } catch (Exception e) {
            throw new ControllerException(SystemErrorCode.SYS_ERR,e);
        }


        return rt;

    }






}
